﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Framework;
using DotNetNuke.Modules.PowerShell.Contexts;
using DotNetNuke.UI.Utilities;
using DotNetNuke.Modules.PowerShell.Args;
using DotNetNuke.Modules.PowerShell.Utils;

namespace DotNetNuke.Modules.PowerShell
{
	public partial class Console : PortalModuleBase, IClientAPICallbackEventHandler
	{
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			jQuery.RequestRegistration();
			this.Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "powershell", this.TemplateSourceDirectory + "/js/console.js");
		}

		public string RaiseClientAPICallbackEvent(string eventArgument)
		{
			string result = string.Empty;
			if (!string.IsNullOrEmpty(eventArgument))
			{
				IContextArgs args = new ContextArgs(eventArgument);

				var context = ContextHelper.CreateContext(args.ContextName);

				if (context != null)
				{
					context.Process(args);
					result = context.Result;
				}
				else
				{
					throw new Exception(string.Format("Unrecognized Command '{0}'", args.ContextName));
				}
			}
			return result;
		}
	}
}